package com.cc.Aop;


import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


import java.util.Arrays;
import java.util.List;



//该类被spring管理
@Component
//设置当前类为AOP切面类
@Aspect
public class MyAdvice {


//    private static final Logger logger = Logger.getLogger(MyAdvice.class);

    //定义切入点
//    @Pointcut("execution(void com.cc.service.Impl.BookService.addBook(String,String,double))")
    @Pointcut("execution(* com.cc.service.Impl.*.*(..))")
    private void pt(){}

    //制作切面
    //绑定切入点与通知关系,并指定添加到原始连接点具体执行位置
    @Before("pt()")
    public void method(){
        System.out.println("方法开始调用时间"+ System.currentTimeMillis());
    }

    @Before("pt()")
    public void BeforeMethod(JoinPoint jp){
        System.out.println("当前调用的类是" +jp.getTarget() + "--" +
                "调用的方法是" +jp.getSignature().getName() + "----");

        System.out.println("传递参数:");
        List<Object> list = Arrays.asList(jp.getArgs());
        list.stream().forEach(o -> System.out.println(o));
    }

    @After("pt()")
    public void AfterMethod(){
        System.out.println("方法结束调用时间"+ System.currentTimeMillis());
    }
    //环绕通知 around
    @Around("pt()")
    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("环绕通知开始");

        System.out.println("环绕通知:1." + pjp.getTarget());

        System.out.println("环绕通知:2." +pjp.getSignature().getName());

        //获取参数
        System.out.println("传入参数:" + Arrays.toString(pjp.getArgs()));
        //表示对原始操作的调用
        Object proceed = pjp.proceed();

        System.out.println("环绕通知结束");

        return proceed;
    }
}
